package com.edu.koTA07.takingOrder;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemClickListener;

import com.edu.koTA07.XmlDataSet.BarangXmlDataSet;
import com.edu.koTA07.adapter.BarangListAdapter;
import com.edu.koTA07.bean.BarangBean;
import com.edu.koTA07.bean.KategoriBarangBean;
import com.edu.koTA07.utils.BarangUtils;
import com.edu.koTA07.utils.KategoriBarangUtils;
public class ListBarang extends ListActivity {	
    private ListView barangList;
    BarangXmlDataSet barang = null;
    Spinner spinner;
    
    ArrayList<BarangBean> currentData;
	//SQLiteDatabase database;
	//SQLHelper helper;
	BarangBean barangBean;
	BarangListAdapter barangListAdapter;
	ArrayList<KategoriBarangBean> getDataKategori;
	BarangUtils util;
	KategoriBarangUtils kategoriBarangUtil;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_barang);
		util = new BarangUtils(this);

		kategoriBarangUtil = new KategoriBarangUtils(this);
		
		init();
	}
	
	
	@SuppressWarnings("unchecked")
	public void init(){
		
        
        
        getDataKategori = new ArrayList<KategoriBarangBean>();
        getDataKategori = kategoriBarangUtil.loadData();
        String [] ret = new String[getDataKategori.size()];
		for(int i = 0;i<getDataKategori.size();i++){
			//ret[i][0] = getDataKategori.get(i).getNamaKategoriBarang();
			ret[i] = getDataKategori.get(i).getNamaKategoriBarang();
		}
		
		spinner = (Spinner) findViewById(R.id.kategoriBarangSpinner);
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this,
       		android.R.layout.simple_spinner_item, ret);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(
					AdapterView adapterView,
					View view, int i, long l) {
					String idKategoriBarang = getDataKategori.get(spinner.getSelectedItemPosition()).getIDKategoriBarang();
					updateListBarang(idKategoriBarang);
			}

			public void onNothingSelected(
					AdapterView adapterView) {
				return;
			}
		});
	}
    
	public void updateListBarang(String idKategoriBarang){
		barangList = (ListView) findViewById(android.R.id.list);
        currentData = new ArrayList<BarangBean>();
        currentData = util.loadData(idKategoriBarang);
        barangListAdapter = new BarangListAdapter(this, currentData);
        barangList.setAdapter(barangListAdapter);
        barangList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
			        int position, long id) {
				BarangBean b = barangListAdapter.getItem(position);
				onShowData(b, ListBarang.this);
				
			}

		});	
	}
	
    AlertDialog alert;
    public void onShowData(BarangBean brng, Context ctx) {
		final BarangBean thisItem = brng;		
		alert = new AlertDialog.Builder(ctx).setIcon(R.drawable.icon)
	        .setTitle("Display Data")
	        .setMessage(" ------------ Barang -------------\n"
	        		  + "ID: "+thisItem.getIDBarang()+"\n" 
	        		  + "Nama Barang: "+thisItem.getNamaBarang()+"\n"
	        		  + "Harga Barang: "+thisItem.getHargaBarang()+"\n")
	        .setNegativeButton("Close", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	               alert.cancel();
	            }
	        }).create();			
		alert.show();
	}
	
}   

